import { Column, Entity, Index, OneToMany } from 'typeorm';
import { BaseEntity } from '../../../common/entities/base.entity';

export enum TaskStatus {
  PENDING = 'pending', // 等待中
  PROCESSING = 'processing', // 处理中
  COMPLETED = 'completed', // 已完成
  FAILED = 'failed', // 失败
}

export enum TaskType {
  NOVEL_PARSE = 'novel-parse', // 小说解析
  CHAPTER_CONTENT = 'chapter-content', // 章节内容抓取
}

@Entity('novel_parse_tasks')
@Index(['novelId'])
@Index(['status'])
@Index(['taskType'])
export class NovelParseTask extends BaseEntity {
  @Column({ type: 'varchar', length: 100, comment: '任务ID' })
  taskId: string;

  @Column({ type: 'varchar', length: 50, comment: '任务类型' })
  taskType: TaskType;

  @Column({ type: 'int', nullable: true, comment: '小说ID' })
  novelId: number | null;

  @Column({ type: 'varchar', length: 200, nullable: true, comment: '小说标题' })
  novelTitle: string | null;

  @Column({ type: 'varchar', length: 500, nullable: true, comment: '原始链接' })
  originalUrl: string | null;

  @Column({
    type: 'varchar',
    length: 20,
    default: TaskStatus.PENDING,
    comment: '任务状态',
  })
  status: TaskStatus;

  @Column({ type: 'int', default: 0, comment: '总章节数' })
  totalChapters: number;

  @Column({ type: 'int', default: 0, comment: '已处理章节数' })
  processedChapters: number;

  @Column({ type: 'int', default: 0, comment: '成功章节数' })
  successChapters: number;

  @Column({ type: 'int', default: 0, comment: '失败章节数' })
  failedChapters: number;

  @Column({
    type: 'decimal',
    precision: 5,
    scale: 2,
    default: 0,
    comment: '进度百分比',
  })
  progress: number;

  @Column({ type: 'text', nullable: true, comment: '错误信息' })
  errorMessage: string | null;

  @Column({ type: 'json', nullable: true, comment: '任务数据' })
  taskData: any;

  @Column({ type: 'datetime', nullable: true, comment: '开始时间' })
  startedAt: Date | null;

  @Column({ type: 'datetime', nullable: true, comment: '完成时间' })
  completedAt: Date | null;

  @Column({ type: 'int', default: 0, comment: '重试次数' })
  retryCount: number;

  @Column({ type: 'datetime', nullable: true, comment: '下次重试时间' })
  nextRetryAt: Date | null;
}
